<?php

	session_start();
	require("connect.php");
	$link = connect_APO();
	
	if (!session_is_registered('ID')) :
		header('Location: login.php?');
	endif;
	
	$ID=$_SESSION['ID'];
	$phone=$_POST['newPhone'];
	
	$dontadd=0;
	if(strcmp($phone,"")!=0){
		$phone = trim($phone);
		if(strcmp(substr($phone,0,1),"(")==0){
			if(strcmp(substr($phone,4,1),")")==0){
				if(ctype_digit(substr($phone,1,3))){			
					if(strcmp(substr($phone,8,1),"-")==0){
						if(ctype_digit(substr($phone,5,3)) && ctype_digit(substr($phone,9,4))){
						} else { 
							header('Location: changePhoneNumber.php?error=1'); 
							$dontadd=1;
							}
					} else { 
						header('Location: changePhoneNumber.php?error=1'); 
						$dontadd=1;
					}
				} else { 
					header('Location: changePhoneNumber.php?error=1'); 
					$dontadd=1;
				}
			} else { 
				header('Location: changePhoneNumber.php?error=1'); 
				$dontadd=1;
			}
		} else if (strcmp(substr($phone,3,1),"-")==0 && strcmp(substr($phone,7,1),"-")==0){
			if(!(ctype_digit(substr($phone,0,3)) && ctype_digit(substr($phone,4,3)) && ctype_digit(substr($phone,8,4)))){
				header('Location: changePhoneNumber.php?error=1'); 
				$dontadd=1;
			}
		} else if (!(ctype_digit($phone) && (strlen($phone)==10))){
			header('Location: changePhoneNumber.php?error=1');  
			$dontadd=1;
		}
	}
	
	require("header+logoutB.php");
	headerL();
	
	if($dontadd!=1){
		if(strcmp($phone,"")!=0){
			$sql="UPDATE `users` SET `PhoneNumber`='$phone' WHERE `users`.`ID`='$ID'";
			$result=mysql_query($sql);
		}
		echo "<html>
		<link rel='stylesheet' type='text/css' href='webstyle.css' />
		<head>
			<title>Changed Phone Number</title>
			<meta http-equiv='refresh' content='2; URL=profile.php'>
		</head>
		<body>
		<center>
		<font class='mainFont'>Your phone number has been changed. You will be redirected to your profile in 2 seconds.</font>
		</center>
		</body>
		</html>";
	}
	
		require("footer.php");
	footer();
	
	mysql_close($link);
?>